﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MarginTrading.Common;
using MarginTrading.Services;
using MarginTrading.Entity;
using Microsoft.Reporting.WebForms;
using System.Globalization;

namespace MarginTrading.site
{
    public partial class DailyInterestStatement : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["UserName"] == null || Session["UserType"] == null)
            {
                Response.Redirect("Login.aspx?RequestUrl=" + Request.Url.OriginalString);
            }

            if (Session["UserType"].ToString() == "4")
            {
                pnlClientId.Visible = false;
                ReportViewer1.Height = 500;
            }
        }

        protected void SearchBtn_Click(object sender, EventArgs e)
        {
            string clientId = null;

            if (Session["UserName"] == null || Session["UserType"] == null)
            {
                Response.Redirect("Login.aspx?RequestUrl=" + Request.Url.OriginalString);
            }
            else
            {
                if (Session["UserType"].ToString() == "4")
                {
                    pnlClientId.Visible = false;
                    clientId = Session["UserName"].ToString();
                }
                else
                {
                    clientId = txtClientId.Text;
                }
            }

            DateTime fromDate = DateTime.MinValue;
            DateTime toDate = DateTime.MinValue;
            DateTime.TryParseExact(txtFromDate.Text,"dd/MM/yyyy", CultureInfo.CurrentCulture,DateTimeStyles.None, out fromDate);
            DateTime.TryParseExact(txtToDate.Text, "dd/MM/yyyy", CultureInfo.CurrentCulture, DateTimeStyles.None, out toDate);

            LoadReport(clientId, fromDate, toDate);
        }

        private void LoadReport(string clientId, DateTime fromDate, DateTime toDate)
        {
            try
            {
                Client client = ClientService.GetClientByCode(clientId);

                toDate = toDate.AddDays(1);
                toDate = toDate.AddSeconds(-1);
                if (client == null)
                {
                    client = ClientService.GetClientByCode(clientId.ToUpper());
                    if (client == null)
                    {
                        client = new Client();
                    }
                    else
                    {
                        clientId = clientId.ToUpper();
                    }

                }
                ReportDataSource ds = new ReportDataSource();
                ds.Name = "master";
                ds.Value = ReportServices.GetInterestByClientAndDateRange(clientId, fromDate, toDate);
                double cumInterest = ReportServices.GetCumInterest(clientId, fromDate);

                this.ReportViewer1.LocalReport.ReportPath = @"Reports\InterestStatement.rdlc";
                this.ReportViewer1.LocalReport.DataSources.Clear();
                this.ReportViewer1.LocalReport.DataSources.Add(ds);

                Config AgentCodeConfig = ConfigServices.GetConfigByName("Agent Code");
                string AgentCode = "cts";
                if (AgentCodeConfig != null)
                {
                    AgentCode = AgentCodeConfig.Value;
                }

                //TODO:Remove this 
                AgentCode = Reports.AGENT_CODE;

                this.ReportViewer1.LocalReport.SetParameters(new ReportParameter[] {
                        new ReportParameter("ImageLink",AgentCode.ToLower()),
                        new ReportParameter("CumInterest",cumInterest.ToString()),
                        new ReportParameter("CompanyName",Reports.COMPANY_NAME),
                        new ReportParameter("ClientID",client.Code),
                        new ReportParameter("Address",string.Concat(client.Address_1," ",client.Address_2," ",client.Address_3," ",client.Address_4)),
                        new ReportParameter("LoanAccountDate",toDate.ToString(DateFormat.DATE_FORMAT)),
                        new ReportParameter("AdvisorID",client.Advisor),
                        new ReportParameter("Name",string.Concat(client.Initials,
                                                            " ", client.Surname) ),
                        new ReportParameter("ReportTitle",string.Format(Reports.STATEMENT_OF_INTEREST,fromDate.ToString(DateFormat.DATE_FORMAT),toDate.ToString(DateFormat.DATE_FORMAT)))});

                //this.reportViewer1.RefreshReport();
                //ReportViewer1.SetDisplayMode(DisplayMode.PrintLayout);
                //reportViewer1.ZoomMode = ZoomMode.Percent;
                //reportViewer1.ZoomPercent = 100;
            }
            catch (Exception e)
            {
                ErrorLog.WriteToEventLog(e.Message);
            }
        }
    }
}
